const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
// postMessage
const rootDir = '/Users/Administrator/Desktop/test/principle_of_applet/router/wx-miniprograme-framework/';
var app = http.createServer((req, res) => {
  /*
   * 底层原理 postMessage发请求的时候
   */
  const filePath = path.resolve(rootDir + req.url);
  fs.readFile(filePath, 'utf-8', (err, content) => {
    if (err) {
      res.write('');
      res.end();
      return;
    }
    res.write(content);
    res.end();
  })
  /*
   * 登录逻辑
   */
  // if(/login/.test(req.url)) {
  //   https.get(`https://api.weixin.qq.com/sns/jscode2session?appid=wx3fc9a3f9b4ce8209&secret=ce50e36aece432818e0d6c8d60a1339d&js_code=${req.url.split('code=')[1]}&grant_type=authorization_code`,function(resHandler) {
  //     var str = '';
  //     resHandler.on('data',(chunk) => {
  //       str += chunk;
  //     })
  //     resHandler.on('end',() => {
  //       console.log('weixin return data::::',str);
  //       res.write(str);
  //       res.end();
  //     })
  //   })
  // }else {
  //   fs.readFile(__dirname+'/mock.json',function(err,content) {
  //     res.write(content);
  //     res.end();
  //   });
  // }
});
app.listen(8999);
console.log('服务器启动')